package org.array.struts.util;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.array.struts.action.ActionMapping;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import org.array.struts.annotation.Controller;
import org.array.struts.annotation.RequestMapping;

public class StrutsConfig {

	@SuppressWarnings("unchecked")
	public static Map<String, ActionMapping> getConfig(String filepath){
		
		Map<String, ActionMapping> map = new HashMap<String, ActionMapping>();
		try {
			Document doc = new SAXReader().read(new File(filepath));
			Element root = doc.getRootElement();
			Iterator<Element> elementIte = root.elementIterator();
			
			while (elementIte.hasNext()) {
				ActionMapping action = new ActionMapping();
				Element actions = elementIte.next();
				action.setPath(actions.attributeValue("path"));
				action.setType(actions.attributeValue("type"));
				action.setName(actions.attributeValue("name"));

				Iterator<Element> forwards = actions.elementIterator("forward");
				Map<String,String> forwardMap = new HashMap<String, String>();

				while(forwards.hasNext()){
				  Element els = forwards.next();
				  forwardMap.put(els.attributeValue("name"), els.getText());
				}
				
				action.setForward(forwardMap);
				map.put(actions.attributeValue("path"), action);				
			}
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return map;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, ActionMapping> loadFromAnnotation(String filepath){
		
		Map<String, ActionMapping> map = new HashMap<String, ActionMapping>();
		try {
			Document doc = new SAXReader().read(new File(filepath));
			Element root = doc.getRootElement();
			//读取struts.xml中scan的扫码类路径配置
			Element ele = root.element("scan");
			String[] packages = ele.attributeValue("package").split(",");
			
			List<Class> list = new ArrayList<Class>();
			String classpath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
			for(String pack : packages){
				File file = new File(classpath+pack.replace(".", "/"));
				File[] files = file.listFiles();
				for(File f : files){
					if(!f.isDirectory()){
						try {
							String fname = f.getName().substring(0, f.getName().lastIndexOf("."));
							list.add(Class.forName(pack+"."+fname));
						} catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			
			for(Class clazz : list){
				//获取Action类的Controller注解
				Controller controller = (Controller) clazz.getAnnotation(Controller.class);
				if(controller != null){
					//获取Action类的RequestMapping注解
					RequestMapping req_class = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
					Method[] methods = clazz.getMethods();
					for(Method m : methods){
						//方法的RequestMapping注解
						RequestMapping req = m.getAnnotation(RequestMapping.class);
						if(req != null){
							StringBuffer paraNames = new StringBuffer();
							for(Class para : m.getParameterTypes()){
								//方法的参数
								paraNames.append(","+para.getName());
							}
							String url = req_class.value() + req.value();
							
							ActionMapping actionMapping = new ActionMapping();
							actionMapping.setPath(url);
							actionMapping.setType(clazz.getName());
							//暂时写死
							actionMapping.setName("org.array.struts.test.TestForm");

							map.put(url, actionMapping);
						}
					}
				}
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return map;
	}
}
